//
//  SYPresentationController.swift
//  SingWeibo
//
//  Created by shiying on 16/1/1.
//  Copyright © 2016年 shiying. All rights reserved.
//

import UIKit
let CZPresentationControllerDismissNotificaiton = "CZPresentationControllerDismissNotificaiton"

class SYPresentationController: UIPresentationController {

    override func containerViewWillLayoutSubviews() {
        super.containerViewWillLayoutSubviews()
        
        // 将背景视图添加到容器
        containerView?.addSubview(backGroundView)
        backGroundView.ff_Fill(containerView!)
        
        // 将背景图片带到最后
        containerView?.sendSubviewToBack(backGroundView)
    
    
        //获取modal 出来的控制器的view
        let modalView = presentedView()
        
        //获取modal 出来的控制器的view的大小
        modalView?.frame = CGRect(x: 100, y: 56, width: 200, height: 300)
    }
    
    func tapBackGrounViewClick() {
        // 获取到Modal出来的控制器，并关闭
        presentedViewController.dismissViewControllerAnimated(true, completion: nil)
        
        // 需要将首页的title按钮转下去，使用通知
        NSNotificationCenter.defaultCenter().postNotificationName(CZPresentationControllerDismissNotificaiton, object: self)
    
    
    }
    
    private lazy var backGroundView: UIView = {
        let view = UIView()
        
        view.backgroundColor = UIColor(white: 0.3, alpha: 0.2)
        
        let tap = UITapGestureRecognizer(target: self, action: "tapBackGrounViewClick")
        view.addGestureRecognizer(tap)
        
        return view
    }()
}
